package com.erbao.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.util.concurrent.Executors;

/**
 * @author CaoBaoYin
 * @since 2022/3/9 21:22
 */
public class Main01 {
    public static void main(String[] args) {
        // 创建工厂
        LongEventFactory longEventFactory = new LongEventFactory();
        // 队列大小
        int ringBufferSize = 1024;
        // 创建disruptor。线程工厂里创建线程，在这个线程里调用消费者，不同的消费者会在不同的线程
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(longEventFactory, ringBufferSize, Executors.defaultThreadFactory());
        // 指定消费者
        disruptor.handleEventsWith(new LongEventHandler());

        // 启动
        disruptor.start();

        // 生产event
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        long sequence = ringBuffer.next();
        LongEvent event = ringBuffer.get(sequence);
        event.setValue(1000L);
        ringBuffer.publish(sequence);

        /*try {
            disruptor.shutdown(2L, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            e.printStackTrace();
        }*/

        /*try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}
